package com.alibaba.dingtalk.openapi.demo;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 
 * 上传工具类
 */
public class UploadUtil
{
       public static void main(String args[]) throws ClientProtocolException, IOException, URISyntaxException{
	   URI url=new URI("https://oapi.dingtalk.com/file/upload/single?access_token=c7f31bd742e430719c0fa874ad0e2c5f&agent_id=102769385&file_size=9728");
	   HttpPost httpPost = new HttpPost(url);

	  //Log.debug("post url:"+url);

	   httpPost.setHeader("User-Agent","SOHUWapRebot");

	   httpPost.setHeader("Accept-Language","zh-cn,zh;q=0.5");

	   httpPost.setHeader("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.7");

	   httpPost.setHeader("Connection","keep-alive");

	    

	   MultipartEntity mutiEntity = new MultipartEntity();

	   File file = new File("f:/test.doc");

	   mutiEntity.addPart("desc",(ContentBody) new StringBody("美丽的西双版纳", Charset.forName("utf-8")));

	   mutiEntity.addPart("pic", new FileBody(file));

	    

	   CloseableHttpClient httpClient = HttpClients.createDefault();

	   httpPost.setEntity(mutiEntity);

	   HttpResponse  httpResponse = httpClient.execute(httpPost);

	   HttpEntity httpEntity =  httpResponse.getEntity();

	   String content = EntityUtils.toString(httpEntity);
   }
}
